package leecode.二分法;

public class 寻找峰值 {
    public static void main(String[] args) {


    }

    static class Soulution {
        public int search(int[] nums) {
            int n = nums.length;
            if (n == 1) {
                return 0;
            }
            if (nums[0] > nums[1]) {
                return 0;
            }
            if (nums[n - 1] > nums[n - 2]) {
                return n - 1;
            }
            int left = 1, right = n - 2;//通过上面判断，n=length-1 和 n=0不属于答案，所以有边界从n-2 左边界从1
            while (left <= right) {
                int mid = (left + right) / 2;
                if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) {
                    return mid;
                }
                if (nums[mid] < nums[mid + 1]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            return -1;
        }

    }
}
